/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f4xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint16_t Key_Status=0x0000u;
Key_StatusTypeDef KEYF1_Status,KEYF2_Status;
extern TIM_HandleTypeDef htim3;
extern uint16_t HMC5883LYaw,GetYawBusy;
extern uint8_t uart2value;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_tim3_ch1_trig;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim4;
extern TIM_HandleTypeDef htim5;
extern TIM_HandleTypeDef htim9;
extern UART_HandleTypeDef huart2;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M4 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */

  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */

  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */

  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */

  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */

  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
	/*KEY SERVICE */
  static uint16_t Interval = 10;
  /* 
    the systick is a special timer which all stm32 chips have 
    the interrput function, SysTick_Handler will be trigged every 1ms
    here the function is used to judge the keystatus when keys are pressed and trig EXTI
    we can get the last keystatus by using HAL_GetKeyStatus()
  */

  /* judge for every 5ms */
  if (Interval++ > 5)
  {
    Interval = 0;
    /* when keys are pressed their Pressing_Interval will change into 1 in EXTI IRQ function */
    if ((KEYF1_Status.Pressing_Interval) || (KEYF1_Status.Release_Interval))
    {
      /* read PIN to get if the key is released */
      if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin))
      {
        if (KEYF1_Status.Release_Interval == 0)
          KEYF1_Status.Release_Interval = 1;
        KEYF1_Status.Pressing_Interval = 0;
      }
      /* judge the period of pressing and longpressed */
      if (KEYF1_Status.Pressing_Interval)
      {
        KEYF1_Status.Pressing_Interval++;
        if (KEYF1_Status.Pressing_Interval > 80)
        {
          Key_Status = KEYF1_LONGPRESSED;
          KEYF1_Status.Pressing_Interval = 0;
        }
      }
      if (KEYF1_Status.Release_Interval)
      {
        KEYF1_Status.Release_Interval++;
        if (KEYF1_Status.Release_Interval > 20)
        {
          Key_Status = KEYF1_PRESSED;
          KEYF1_Status.Release_Interval = 0;
        }
      }
    }
    /* ditto */
    if ((KEYF2_Status.Pressing_Interval) || (KEYF2_Status.Release_Interval))
    {
      if (HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin))
      {
        if (KEYF2_Status.Release_Interval == 0)
          KEYF2_Status.Release_Interval = 1;
        KEYF2_Status.Pressing_Interval = 0;
      }
      if (KEYF2_Status.Pressing_Interval)
      {
        KEYF2_Status.Pressing_Interval++;
        if (KEYF2_Status.Pressing_Interval > 80)
        {
          Key_Status = KEYF2_LONGPRESSED;
          KEYF2_Status.Pressing_Interval = 0;
        }
      }
      if (KEYF2_Status.Release_Interval)
      {
        KEYF2_Status.Release_Interval++;
        if (KEYF2_Status.Release_Interval > 20)
        {
          Key_Status = KEYF2_PRESSED;
          KEYF2_Status.Release_Interval = 0;
        }
      }
    }
  }
  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F4xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f4xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles DMA1 stream4 global interrupt.
  */
void DMA1_Stream4_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream4_IRQn 0 */

  /* USER CODE END DMA1_Stream4_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_tim3_ch1_trig);
  /* USER CODE BEGIN DMA1_Stream4_IRQn 1 */

  /* USER CODE END DMA1_Stream4_IRQn 1 */
}

/**
  * @brief This function handles TIM2 global interrupt.
  */
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */

  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */
	HAL_TIM2_Callback();
  /* USER CODE END TIM2_IRQn 1 */
}

/**
  * @brief This function handles TIM4 global interrupt.
  */
void TIM4_IRQHandler(void)
{
  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */
  HAL_TIM_IRQHandler(&htim4);
  /* USER CODE BEGIN TIM4_IRQn 1 */
  HAL_TIM4_Callback();
  /* USER CODE END TIM4_IRQn 1 */
}

/**
  * @brief This function handles TIM4 global interrupt.
  */
void TIM5_IRQHandler(void)
{
  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */
  HAL_TIM_IRQHandler(&htim5);
  /* USER CODE BEGIN TIM4_IRQn 1 */
  HAL_TIM5_Callback();
  /* USER CODE END TIM4_IRQn 1 */
}

/**
  * @brief This function handles TIM4 global interrupt.
  */
void TIM9_IRQHandler(void)
{
  /* USER CODE BEGIN TIM4_IRQn 0 */

  /* USER CODE END TIM4_IRQn 0 */
  HAL_TIM_IRQHandler(&htim9);
  /* USER CODE BEGIN TIM4_IRQn 1 */
  HAL_TIM9_Callback();
  /* USER CODE END TIM4_IRQn 1 */
}

/**
  * @brief This function handles USART6 global interrupt.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART6_IRQn 0 */
  static uint16_t countOfGetBuffer = 0, endflag = 0;
  static uint8_t getBuffer[50];
  uint16_t k = 0, k1 = 0, temp = 0;
  extern uint16_t SettingYaw;
  /* USER CODE END USART6_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART6_IRQn 1 */

  /* write the receiving data to the buffer */
  getBuffer[countOfGetBuffer] = uart2value;
  /* set a flag when encountering the endbyte, '*' */
  if (getBuffer[countOfGetBuffer] == '*')
    endflag = 1;
  /* add counter, but no more than 50 */
  countOfGetBuffer = (countOfGetBuffer + 1) % 50;
  if (endflag == 1 && countOfGetBuffer >= 3)
  {
    /* find the startbyte */
    while (getBuffer[k] != '$' && k < 50)
      k++;
    /* if there is no starybyte in the buffer, reset the function */
    if (k >= 50)
    {
      for (uint8_t i = 0; i < 50; i++)
        getBuffer[i] = 0;
      endflag = 0;
      countOfGetBuffer = 0;
      return;
    }
    /* get the true data */
    k1 = k + 1;
    while (k1 - k <= 3 && getBuffer[k1] >= '0' && getBuffer[k1] <= '9')
    {
      temp *= 10;
      temp += getBuffer[k1] - '0';
      k1++;
    }
    SettingYaw = temp;
    printf("[tcp]%d\r\n", temp);
    for (uint8_t i = 0; i < 50; i++)
      getBuffer[i] = 0;
    endflag = 0;
    countOfGetBuffer = 0;
  }
  HAL_UART_Receive_IT(&huart2, (uint8_t *)&uart2value, 1);

  /* USER CODE END USART6_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/**
  * @brief This function contain code in TIM2 interrput
  */
void HAL_TIM2_Callback(void)
{
	GetYawBusy=1;
	HMC5883LYaw=HAL_HMC5883L_GetYaw();
	GetYawBusy=0;
}

/**
  * @brief This function contain code in TIM2 interrput
  */
void HAL_TIM4_Callback(void)
{
  TIM4_Pulse_Select();
}

/**
  * @brief This function contain code in TIM2 interrput
  */
void HAL_TIM5_Callback(void)
{
	
}

/**
  * @brief This function contain code in TIM2 interrput
  */
void HAL_TIM9_Callback(void)
{
	
	
}

/**
  * @brief This function handles EXTI line4 interrupt.
  */
void EXTI4_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI4_IRQn 0 */

  /* USER CODE END EXTI4_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
  /* USER CODE BEGIN EXTI4_IRQn 1 */
  if ((KEYF1_Status.Release_Interval) && (KEYF1_Status.Release_Interval <= 20))
  {
    Key_Status = KEYF1_DOUBLEPRESSED;
    KEYF1_Status.Release_Interval = 0;
  }
  else
    KEYF1_Status.Pressing_Interval = 1;

  /* USER CODE END EXTI4_IRQn 1 */
}

/**
  * @brief This function handles EXTI line[9:5] interrupts.
  */
void EXTI9_5_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI9_5_IRQn 0 */

  /* USER CODE END EXTI9_5_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
  /* USER CODE BEGIN EXTI9_5_IRQn 1 */
  if ((KEYF2_Status.Release_Interval) && (KEYF2_Status.Release_Interval <= 20))
  {
    Key_Status = KEYF2_DOUBLEPRESSED;
    KEYF2_Status.Release_Interval = 0;
  }
  else
    KEYF2_Status.Pressing_Interval = 1;

  /* USER CODE END EXTI9_5_IRQn 1 */
}

/**
  * @brief  Get the key status and clean it to 0(not pressed)
  * @retval int
  */
uint16_t HAL_GetKeyStatus(void)
{
  uint16_t ans=Key_Status;
  Key_Status=0;
  return ans;
}

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
